前言 Activity一般是安卓开发接触到的第一个重要组件,代表着与用户交互的活动视窗,活动与活动之间往往不是相互独立的,startActivity()
就是从一个活动跳转到另一个活动的桥梁。而对于startActivity()
的了解,我们往往只停留在它的使用上,很少去深究它的原理 。
今天,就让我们跟随着源码,一步一步的追溯它的调用流程,初步了解下它的调用链,后续我们还可以继续挖掘,关于应用启动、启动模式、hook启动流程等相关技术支持。
此系列博客在未声明API版本情况下,将使用API30 版本的AndroidSDK源码。
源码查看工具使用AndroidStudio。源码来源%AndroidSDK%/sources
。一般方式:备份一份source目录,使用AndroidStudio Open你想观察的api版本的源码目录。
源码分析 Activity.startActivity() 我们日常编写的activity.startActivity(intent)
的代码,实际是调用以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 {@link android.app.Activity#startActivity} @Override public void startActivity (Intent intent) { this .startActivity(intent, null ); } @Override public void startActivity (Intent intent, @Nullable Bundle options) { if (options != null ) { startActivityForResult(intent, -1 , options); } else { startActivityForResult(intent, -1 ); } }
Activity.startActivityForResult() 有上述代码可知,startActivity() 实际是调用 startActivityForResult():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 {@link android.app.Activity#startActivityForResult} public void startActivityForResult (@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { if (mParent == null ) { options = transferSpringboardActivityOptions(options); Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this , mMainThread.getApplicationThread(), mToken, this , intent, requestCode, options); if (ar != null ) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } if (requestCode >= 0 ) { mStartedActivity = true ; } cancelInputsAndStartExitTransition(options); } else { if (options != null ) { mParent.startActivityFromChild(this , intent, requestCode, options); } else { mParent.startActivityFromChild(this , intent, requestCode); } } }
此处,我们抛开其他代码,关注这里的if else 最终都是调用mInstrumentation.execStartActivity(),从字面意义上说,它代表“执行启动活动”,显然我们应当关注此方法。
Instrumentation.execStartActivity() Instrumentation类。此处我们不做详细分析,但应当知道的是,它的作用及能力:
实现应用程序插装代码的基类。在启动插装的情况下运行时,这个类将在任何应用程序代码之前为您实例化,允许您监视系统与应用程序之间的所有交互。通过AndroidManifest.xml的< Instrumentation >标签向系统描述了一个Instrumentation实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 {@link android.app.Instrumentation#execStartActivity} @UnsupportedAppUsage public ActivityResult execStartActivity ( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) { IApplicationThread whoThread = (IApplicationThread) contextThread; try { intent.migrateExtraStreamToClipData(who); intent.prepareToLeaveProcess(who); int result = ActivityTaskManager.getService().startActivity(whoThread, who.getBasePackageName(), who.getAttributionTag(), intent, intent.resolveTypeIfNeeded(who.getContentResolver()), token, target != null ? target.mEmbeddedID : null , requestCode, 0 , null , options); checkStartActivityResult(result, intent); } catch (RemoteException e) { throw new RuntimeException("Failure from system" , e); } return null ; }
这里,我们可以看到,Instrumentation.execStartActivity()调用了ActivityTaskManager.getService().startActivity()将启动活动委托给了某个“服务”执行,我们来看看这个“服务”是什么。
ActivityTaskManager.getService() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 {@link android.app.ActivityTaskManager} public static IActivityTaskManager getService () { return IActivityTaskManagerSingleton.get(); } @UnsupportedAppUsage(trackingBug = 129726065) private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton = new Singleton<IActivityTaskManager>() { @Override protected IActivityTaskManager create () { final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE); return IActivityTaskManager.Stub.asInterface(b); } };
这里代码的作用其实是通过获取IBinder类型的服务生产一个启动活动的代理类,实现方式: 使用的是名为ACTIVITY_TASK_SERVICE的IBinder生产了一个IActivityTaskManager 。
使用过AIDL的同学应该看出来了,这里类似的,也是通过跨进程通信的方式操作的。
IActivityTaskManager.startActivity() 由于我们没办法直接获取到IActivityTaskManager的实现类,我们通过搜索IActivityTaskManager.Stub
的实现类来寻找前面提到的“服务”具体是什么。
由上图可知,这个“服务”应该就是com.android.server.wm.ActivityTaskManagerService
,我们找到startActivity()方法,验证其入参是否一致:
1 2 3 4 5 6 7 8 9 10 {@link com.android.server.wm.ActivityTaskManagerService} @Override public final int startActivity (IApplicationThread caller, String callingPackage, String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) { return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, UserHandle.getCallingUserId()); }
通过入参比对,基本确认此方法是Instrumentation.execStartActivity()中调用ActivityTaskManager.getService().startActivity()
的后续流程了。
ActivityTaskManagerService.startActivityAsUser
startActivityAsUser包含一系列重载方法,最终都是执行以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 {@link com.android.server.wm.ActivityTaskManagerService} private int startActivityAsUser (IApplicationThread caller, String callingPackage, @Nullable String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) { assertPackageMatchesCallingUid(callingPackage); enforceNotIsolatedCaller("startActivityAsUser" ); userId = getActivityStartController().checkTargetUser(userId, validateIncomingUser, Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser" ); return getActivityStartController().obtainStarter(intent, "startActivityAsUser" ) .setCaller(caller) .setCallingPackage(callingPackage) .setCallingFeatureId(callingFeatureId) .setResolvedType(resolvedType) .setResultTo(resultTo) .setResultWho(resultWho) .setRequestCode(requestCode) .setStartFlags(startFlags) .setProfilerInfo(profilerInfo) .setActivityOptions(bOptions) .setUserId(userId) .execute(); }
这里主要通过工厂生产出ActivityStarter对象,并调用execute()执行。
ActivityStarter.execute() 由该方法注释我们可知其作用:
根据前面提供的请求参数解析必要的信息,并执行开始启动活动旅程的请求。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 {@link com.android.server.wm.ActivityStarter} {@link } int execute () { try { int res; synchronized (mService.mGlobalLock) { res = executeRequest(mRequest); Binder.restoreCallingIdentity(origId); return getExternalResult(mRequest.waitResult == null ? res : waitForResult(res, mLastStartActivityRecord)); } } finally { onExecutionComplete(); } }
而这个方法的核心是调用executeRequest(mRequest)
,由于代码较长,此处省略具体实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 {@link com.android.server.wm.ActivityStarter} private int executeRequest (Request request) { final IApplicationThread caller = request.caller; final IBinder resultTo = request.resultTo; boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho, requestCode, callingPid, callingUid, callingPackage, callingFeatureId, request.ignoreTargetSecurity, inTask != null , callerApp, resultRecord, resultStack); abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid, callingPid, resolvedType, aInfo.applicationInfo); abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid, callingPackage); final ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid, callingPackage, callingFeatureId, intent, resolvedType, aInfo, mService.getGlobalConfiguration(), resultRecord, resultWho, requestCode, request.componentSpecified, voiceSession != null , mSupervisor, checkedOptions, sourceRecord); mLastStartActivityRecord = r; mService.onStartActivitySetDidAppSwitch(); mController.doPendingActivityLaunches(false ); mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession, request.voiceInteractor, startFlags, true , checkedOptions, inTask, restrictedBgActivity, intentGrants); if (request.outActivity != null ) { request.outActivity[0 ] = mLastStartActivityRecord; } return mLastStartActivityResult; }
executeRequest 做了基本的启动检查,创建包含IApplicationThread对象的Activity的表示类ActivityRecord(历史堆栈中的一个条目,表示一个活动),并最终调用 startActivityUnchecked() 方法,startActivityUnchecked()中主要调用 startActivityInner() 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 {@link com.android.server.wm.ActivityStarter} int startActivityInner (final ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags, boolean doResume, ActivityOptions options, Task inTask, boolean restrictedBgActivity, NeededUriGrants intentGrants) { final Task targetTask = reusedTask != null ? reusedTask : computeTargetTask(); final boolean newTask = targetTask == null ; mTargetStack.startActivityLocked(mStartActivity, topStack.getTopNonFinishingActivity(), newTask, mKeepCurTransition, mOptions); if (mDoResume) { final ActivityRecord topTaskActivity = mStartActivity.getTask().topRunningActivityLocked(); if (!mTargetStack.isTopActivityFocusable() || (topTaskActivity != null && topTaskActivity.isTaskOverlay() && mStartActivity != topTaskActivity)) { mTargetStack.ensureActivitiesVisible(null , 0 , !PRESERVE_WINDOWS); mTargetStack.getDisplay().mDisplayContent.executeAppTransition(); } else { if (mTargetStack.isTopActivityFocusable() && !mRootWindowContainer.isTopDisplayFocusedStack(mTargetStack)) { mTargetStack.moveToFront("startActivityInner" ); } mRootWindowContainer.resumeFocusedStacksTopActivities( mTargetStack, mStartActivity, mOptions); } } mRootWindowContainer.updateUserStack(mStartActivity.mUserId, mTargetStack); mSupervisor.mRecentTasks.add(mStartActivity.getTask()); mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(), mPreferredWindowingMode, mPreferredTaskDisplayArea, mTargetStack); return START_SUCCESS; }
由于这里代码较长,故忽略了大量代码,我们对这个方法的功能做一个简单的总结:
根据当前栈的情况,判断目标Activity入栈操作:是否需要新栈,是否栈顶复用,是否弹栈等
转场动画相关操作
判断目标活动是否可获得焦点,执行对应操作
那么,在判断目标活动是否可获得焦点的分支里,我们启动的activity自然是需要焦点(可见可操作)的,所以我们主要关注mRootWindowContainer.resumeFocusedStacksTopActivities(mTargetStack, mStartActivity, mOptions);
.
RootWindowContainer.resumeFocusedStacksTopActivities() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 {@link com.android.server.wm.RootWindowContainer} boolean resumeFocusedStacksTopActivities ( ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) { if (!mStackSupervisor.readyToResume()) { return false ; } boolean result = false ; if (targetStack != null && (targetStack.isTopStackInDisplayArea() || getTopDisplayFocusedStack() == targetStack)) { result = targetStack.resumeTopActivityUncheckedLocked(target, targetOptions); } for (int displayNdx = getChildCount() - 1 ; displayNdx >= 0 ; --displayNdx) { if (!resumedOnDisplay) { final ActivityStack focusedStack = display.getFocusedStack(); if (focusedStack != null ) { result |= focusedStack.resumeTopActivityUncheckedLocked(target, targetOptions); } else if (targetStack == null ) { result |= resumeHomeActivity(null , "no-focusable-task" , display.getDefaultTaskDisplayArea()); } } } return result; }
观察上述代码,主要做了以下事情:
如果目标已展示在栈顶可见区域,则执行resume
如果不存在有效的活动(崩溃或首次启动),则启动首页
如果存在有效活动(存在有焦点的栈),继续执行启动流程
此处,我们以存在有效活动为条件,继续关注启动流程,跟进resumeTopActivityUncheckedLocked();
ActivityStack.resumeTopActivityUncheckedLocked() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 {@link com.android.server.wm.ActivityStack} boolean resumeTopActivityUncheckedLocked (ActivityRecord prev, ActivityOptions options) { if (mInResumeTopActivity) { return false ; } boolean result = false ; try { mInResumeTopActivity = true ; result = resumeTopActivityInnerLocked(prev, options); final ActivityRecord next = topRunningActivity(true ); if (next == null || !next.canTurnScreenOn()) { checkReadyForSleep(); } } finally { mInResumeTopActivity = false ; } return result; }
观察代码,这里主要做了防止递归调用的措施,以及调用了resumeTopActivityInnerLocked()方法,代码较长,此处将做忽略和拆解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 {@link com.android.server.wm.ActivityStack} private boolean resumeTopActivityInnerLocked (ActivityRecord prev, ActivityOptions options) { if (!hasRunningActivity) { return resumeNextFocusableActivityWhenStackIsEmpty(prev, options); } if (next.attachedToProcess()) { }else { if (!next.hasBeenLaunched) { next.hasBeenLaunched = true ; } else { if (SHOW_APP_STARTING_PREVIEW) { next.showStartingWindow(null , false , false ); } if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next); } if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next); mStackSupervisor.startSpecificActivity(next, true , true ); } return true ; }
代码比较长,其中大部分的操作在上述代码的注释中有说明,此处不做过多关注,我们看到与startActivity()有关的代码为mStackSupervisor.startSpecificActivity(next, true, true)
, 我们来看一下它的作用是什么。
ActivityStackSupervisor.startSpecificActivity() Supervisor :主管。这个类字面意思是活动栈的主管,它的作用从注释中可以了解到:
将与层次结构相关的东西移动到RootWindowContainer
将与活动生命周期相关的东西移动到一个新的类ActivityLifeCycler中
移动接口事物到ActivityTaskManagerService。
所有其他的小事情到其他文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 void startSpecificActivity(ActivityRecord r, boolean andResume, boolean checkConfig) { // Is this activity's application already running? // 真是个糟糕的注释,让我想起了偶尔会遇到的一个报错 ┗|`O′|┛ final WindowProcessController wpc = mService.getProcessController(r.processName, r.info.applicationInfo.uid); boolean knownToBeDead = false; // 如果applicationThread存在,则执行真实的启动Activity if (wpc != null && wpc.hasThread()) { try { realStartActivityLocked(r, wpc, andResume, checkConfig); return; } catch (RemoteException e) { Slog.w(TAG, "Exception when starting activity " + r.intent.getComponent().flattenToShortString(), e); } // 如果抛出了死对象异常——重新启动应用程序。 knownToBeDead = true; } r.notifyUnknownVisibilityLaunchedForKeyguardTransition(); final boolean isTop = andResume && r.isTopRunningActivity(); // 若applicationThread不存在,将启动新的进程 mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity"); }
上述代码中,主要做了三次校验:
若目标ApplicationThread对象存在,则继续执行启动流程
若启动流程执行失败,则重启应用程序
若目标ApplicationThread对象不存在,则启动新的进程;此流程暂不做关注,可自行跟进,最终会调用ActivityManagerService.startProcessLocked()及后续ZygoteProcess相关方法。
ActivityStackSupervisor.realStartActivityLocked() ActivityStackSupervisor.realStartActivityLocked()基本算得上启动流程中相当重要的一部分了,基本算得上是调用链的结束,后续将执行目标ActivityThread执行Launch操作,故单独做一小节讲述。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 {@link com.android.server.wm.ActivityStackSupervisor} boolean realStartActivityLocked (ActivityRecord r, WindowProcessController proc, boolean andResume, boolean checkConfig) throws RemoteException { if (!mRootWindowContainer.allPausedActivitiesComplete()) { if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE, "realStartActivityLocked: Skipping start of r=" + r + " some activities pausing..." ); return false ; } final MergedConfiguration mergedConfiguration = new MergedConfiguration( proc.getConfiguration(), r.getMergedOverrideConfiguration()); r.setLastReportedConfiguration(mergedConfiguration); logIfTransactionTooLarge(r.intent, r.getSavedState()); final ClientTransaction clientTransaction = ClientTransaction.obtain( proc.getThread(), r.appToken); final DisplayContent dc = r.getDisplay().mDisplayContent; clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent), System.identityHashCode(r), r.info, mergedConfiguration.getGlobalConfiguration(), mergedConfiguration.getOverrideConfiguration(), r.compat, r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(), r.getSavedState(), r.getPersistentSavedState(), results, newIntents, dc.isNextTransitionForward(), proc.createProfilerInfoIfNeeded(), r.assistToken, r.createFixedRotationAdjustmentsIfNeeded())); final ActivityLifecycleItem lifecycleItem; if (andResume) { lifecycleItem = ResumeActivityItem.obtain(dc.isNextTransitionForward()); } else { lifecycleItem = PauseActivityItem.obtain(); } clientTransaction.setLifecycleStateRequest(lifecycleItem); mService.getLifecycleManager().scheduleTransaction(clientTransaction); proc.onStartActivity(mService.mTopProcessState, r.info); if (mRootWindowContainer.isTopDisplayFocusedStack(stack)) { mService.getActivityStartController().startSetupActivity(); } if (r.app != null ) { r.app.updateServiceConnectionActivities(); } return true ; }
从上述代码及注释我们可知,realStartActivityLocked的主要功能是:
若有正在pause的活动,等待它操作完毕
创建 LaunchActivityItem (启动Activity的策略/状态)并封装成事务
执行启动Activity的事务
更新进程信息及其他相关服务
那么,我们知道activity启动流程的最后两步即是:创建 LaunchActivityItem 及执行 ClientTransaction,这里我们分两各部分介绍,先介绍 ClientTransaction 的调用流程,再介绍 LaunchActivityItem 是什么。
执行LaunchActivityItem事务 ActivityStackSupervisor.realStartActivityLocked()中调用的mService.getLifecycleManager().scheduleTransaction(clientTransaction);
实际是ClientLifecycleManager.scheduleTransaction():
1 2 3 4 5 6 7 8 9 10 {@link com.android.server.wm.ClientLifecycleManager} void scheduleTransaction (ClientTransaction transaction) throws RemoteException { final IApplicationThread client = transaction.getClient(); transaction.schedule(); if (!(client instanceof Binder)) { transaction.recycle(); } }
这里主要调用了ClientTransaction.schedule()
:
1 2 3 4 5 6 7 {@link android.app.servertransaction.ClientTransaction} private IApplicationThread mClient;public void schedule () throws RemoteException { mClient.scheduleTransaction(this ); }
由代码可知,这里调用了IApplicationThread.scheduleTransaction()。这里的IApplicationThread来源于ClientTransaction.obtain()入参,向前追溯则是通过ActivityRecorder获取的,实际是前述启动流程中传递的caller【此处描述可能不够严谨,如有错误请评论或联系作者】。
这里我们首先应该找到 IApplicationThread的实现,如果你对ActivityThread有一定了解的话,可以知道其内部类 ApplicationThread 实现了 IApplicationThread.Stub:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 {@link android.app.ActivityThread} public final class ActivityThread extends ClientTransactionHandler { private class ApplicationThread extends IApplicationThread.Stub { // 忽略其他代码 @Override public void scheduleTransaction(ClientTransaction transaction) throws RemoteException { ActivityThread.this.scheduleTransaction(transaction); } } } public abstract class ClientTransactionHandler { // Schedule phase related logic and handlers. /** Prepare and schedule transaction for execution. */ void scheduleTransaction(ClientTransaction transaction) { transaction.preExecute(this); sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction); } } class H extends Handler { public void handleMessage(Message msg) { switch (msg.what) { case EXECUTE_TRANSACTION: final ClientTransaction transaction = (ClientTransaction) msg.obj; mTransactionExecutor.execute(transaction); if (isSystem()) { // 系统进程内部的客户端事务在客户端循环,而不是在ClientLifecycleManager中循环,以避免在此消息被处理之前被清除。 transaction.recycle(); } // TODO(lifecycler): Recycle locally scheduled transactions. break; } } }
那么,调用IApplicationThread.scheduleTransaction(),实际上是向ActivityThread.H发送了Message,而后ActivityThread.H处理消息并调用了mTransactionExecutor.execute(transaction)
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 {@link android.app.servertransaction.TransactionExecutor} public void execute (ClientTransaction transaction) { executeCallbacks(transaction); executeLifecycleState(transaction); mPendingActions.clear(); if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "End resolving transaction" ); } @VisibleForTesting public void executeCallbacks (ClientTransaction transaction) { final int size = callbacks.size(); for (int i = 0 ; i < size; ++i) { final ClientTransactionItem item = callbacks.get(i); item.execute(mTransactionHandler, token, mPendingActions); item.postExecute(mTransactionHandler, token, mPendingActions); } }
TransactionExecutor.execute()及后续executeCallbacks()最终调用到了 BaseClientRequest.execute(),也就是LaunchActivityItem.execute()。
LaunchActivityItem 在Android API 29及以上SDK中,Google将可以调度和执行的到客户端的回调消息封装为事务,包含Launch、Start、Stop等各种状态。
继承自 ClientTransactionItem 及上层 BaseClientRequest,核心方法为execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions),已知的ClientTransactionHandler 有ActivityThread。
1 2 3 4 5 6 7 8 9 10 11 12 {@link android.app.servertransaction.LaunchActivityItem} @Override public void execute (ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart" ); ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState, mPendingResults, mPendingNewIntents, mIsForward, mProfilerInfo, client, mAssistToken, mFixedRotationAdjustments); client.handleLaunchActivity(r, pendingActions, null ); Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); }
LaunchActivityItem.execute()中主要调用了client.handleLaunchActivity(r, pendingActions, null /* customIntent */)
,也就是调用了ActivityThread的相关方法。
当然的ActivityThread.handleLaunchActivity() 就是实际的新的Activity从创建到展示的流程了。
至此,其实整个从 startActivity的调用到目标Activity创建的调用流程就已经分析完毕了。
最后,我们来画一画整个流程的时序图。
分步骤时序图 Activity->ActivityManagerService
Activity检查及准备阶段
IApplication -> ActivityThread -> Activity
整体流程时序图